home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / lpc10 / filter.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  2KB  |  94 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "filter.h"
  4.  
  5. /*     --------------------------------  FILTER_CREATE  --------------------------------
  6. */
  7.  
  8. FILTER *filter_create(num, nl, den, dl)
  9.  
  10. float *num, *den;            /*  num[0:nl-1]            den[0:dl-1]             */
  11. int nl, dl;                /*  nl: numerator length    dl: denominator length  */
  12.  
  13. {
  14.    int i;
  15.    FILTER *fp;
  16.  
  17.    fp = (FILTER *) malloc(sizeof(FILTER));
  18.  
  19.    fp->nl = nl;
  20.    fp->num = (float *) malloc((unsigned) fp->nl * sizeof(float));
  21.    for (i=0; i<fp->nl; i++) fp->num[i] = num[i];
  22.  
  23.    fp->dl = dl;
  24.    fp->den = (float *) malloc((unsigned) fp->dl * sizeof(float));
  25.    for (i=0; i<fp->dl; i++) fp->den[i] = den[i];
  26.  
  27.    /*   normalize the denominator, if needed   */
  28.    if (fp->den[0] != 1.0)
  29.    {
  30.       for (i=1; i<fp->dl; i++) fp->den[i] = fp->den[i] / fp->den[0];
  31.       for (i=0; i<fp->nl; i++) fp->num[i] = fp->num[i] / fp->den[0];
  32.       fp->den[0] = 1.0;
  33.    }
  34.  
  35.    /*   determine needed buffer length and create the buffer   */
  36.    if (fp->nl > fp->dl) fp->bufl = fp->nl - 1;
  37.    else                 fp->bufl = fp->dl - 1;
  38.    fp->buf = (float *) malloc((unsigned) fp->bufl * sizeof(float));
  39.    fp->buf -= 1;        /*   buf[1:bufl]   */
  40.    for (i=1; i<=fp->bufl; i++) fp->buf[i] = 0.0;
  41.  
  42.    return fp;
  43. }
  44.  
  45. /*     --------------------------------  FILTER  --------------------------------
  46. */
  47.  
  48. float filter(fp, in)
  49.  
  50. FILTER *fp;
  51. float in;
  52.  
  53. {
  54.    int i;
  55.    float out;
  56.  
  57.    for (i=1; i<fp->dl; i++) in -= fp->den[i] * fp->buf[i];    /* denominator */
  58.    out = fp->num[0] * in;
  59.    for (i=1; i<fp->nl; i++) out += fp->num[i] * fp->buf[i];    /* numerator */
  60.    for (i=fp->bufl; i>1; i--) fp->buf[i] = fp->buf[i-1];    /* shift buffer */
  61.    fp->buf[1] = in;
  62.  
  63.    return out;
  64. }
  65.  
  66. /*     --------------------------------  FILTER_STATE_READ  --------------------------------
  67. */
  68.  
  69. float *filter_state_read(fp, n)
  70.  
  71. FILTER *fp;
  72. int *n;
  73.  
  74. {
  75.    *n = fp->bufl;
  76.    return fp->buf + 1;
  77. }
  78.  
  79. /*     --------------------------------  FILTER_STATE_SET  --------------------------------
  80. */
  81.  
  82. int filter_state_set(fp, n, x)
  83.  
  84. FILTER *fp;
  85. int n;
  86. float x[];
  87.  
  88. {
  89.    int i;
  90.    if (n != fp->bufl) return -1;
  91.    for (i=1; i<=fp->bufl; i++) fp->buf[i] = x[i-1];
  92.    return 0;
  93. }
  94.